package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.AbstractButton;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import Client.ChatClientSendThread;
import Mailbox.Mailbox;
import Mailbox.MailboxFTP;

public class GUIstartpage extends JFrame implements MouseListener {
	private static final long serialVersionUID = 27L;

	private JPanel StartWindow; // The start window

	private JButton openChatButton; // Opens the selected chat room
	private JButton createRoomButton; // Creates a new chat room
	private JButton startChatButton; // Starts a private chat
	private JButton exitButton; // Exits the program

	private JScrollPane roomsPane; // Scroll for the list of chat room
	public JList roomsList; // The list of chat rooms
	private DefaultListModel roomsModel;

	private JScrollPane friendsPane;
	public JList friendsList; // The list of friends
	private DefaultListModel friendsModel;
	private HashMap<String, GUIchatroom> chatrooms;
	private HashMap<String, GUIprivate> privteChatrooms;
	private GUIlogin gl;

	private String username;

	ChatClientSendThread client;

	private MailboxFTP mbftp;

	public GUIstartpage(String username, ChatClientSendThread client,
			MailboxFTP mbftp) {
		this.mbftp = mbftp;
		this.username = username;
		this.client = client;
		chatrooms = new HashMap<String, GUIchatroom>();
		privteChatrooms = new HashMap<String, GUIprivate>();
		StartWindow = new JPanel();
		setTitle(username + "'s chatspace");

		openChatButton = new OpenChatButton("Open selected chat room", this);
		createRoomButton = new CreateRoomButton("Create new chat room", this);
		startChatButton = new StartChatButton("Start private conversation",
				this);
		exitButton = new ExitButton("Exit");

		roomsModel = new DefaultListModel();
		roomsList = new JList(roomsModel);

		roomsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		roomsList.setSelectedIndex(0);
		roomsList.addMouseListener(this);
		roomsList.setVisibleRowCount(15);
		roomsList.setPreferredSize(new Dimension(200, 200));

		roomsPane = new JScrollPane(roomsList);

		friendsModel = new DefaultListModel();
		friendsList = new JList(friendsModel);

		friendsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		friendsList.setSelectedIndex(0);
		friendsList.addMouseListener(this);
		friendsList.setVisibleRowCount(15);
		friendsList.setPreferredSize(new Dimension(100, 200));

		friendsPane = new JScrollPane(friendsList);

		StartWindow.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		// The room pane
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 6;
		c.insets = new Insets(10, 0, 10, 10);
		StartWindow.add(roomsPane, c);

		// The friends pane
		c.gridx = 6;
		c.gridy = 0;
		c.gridwidth = 1;
		c.insets = new Insets(10, 0, 10, 0);
		StartWindow.add(friendsPane, c);

		c = new GridBagConstraints();

		// For the openChatbutton
		c.gridx = 0;
		c.gridy = 1;
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(0, 0, 10, 0);
		StartWindow.add(openChatButton, c);

		// For the startChatbutton
		c.gridx = 6;
		c.gridy = 1; // 4
		c.anchor = GridBagConstraints.LINE_END;
		StartWindow.add(startChatButton, c);

		// For the createRoombutton
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 6;
		c.anchor = GridBagConstraints.WEST;
		StartWindow.add(createRoomButton, c);

		// For the exitbutton
		c.gridx = 6;
		c.gridy = 2;
		c.anchor = GridBagConstraints.EAST;
		StartWindow.add(exitButton, c);

		add(StartWindow, BorderLayout.CENTER);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocation(200, 200);
		setSize(750, 600);
		setVisible(true);

	}



	private class OpenChatButton extends JButton implements ActionListener {
		private static final long serialVersionUID = 27L;
		private GUIstartpage sp;

		public OpenChatButton(String name, GUIstartpage sp) {
			super(name);
			addActionListener(this);
			this.sp = sp;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String chatRoom = null;
			try {
				chatRoom = roomsList.getSelectedValue().toString();
			} catch (NullPointerException e) {
				return;
			}

			if (chatRoom == null) {
				return;
			}

			if (chatRoom.contains("PrivateChat")) {
				if (!privteChatrooms.containsKey(chatRoom)) {
					privteChatrooms.put(chatRoom, new GUIprivate(client,
							username, chatRoom, mbftp, sp));
					client.send("J " + chatRoom);
				}
			} else {
				if (!chatrooms.containsKey(chatRoom)) {
					chatrooms.put(chatRoom, new GUIchatroom(username, chatRoom,
							client, sp));
					client.send("J " + chatRoom);
				}
			}
		}

	}

	private class CreateRoomButton extends JButton implements ActionListener {
		private static final long serialVersionUID = 27L;
		private GUIstartpage sp;

		public CreateRoomButton(String name, GUIstartpage sp) {
			super(name);
			addActionListener(this);
			this.sp = sp;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			new GUIcreatechatroom(client, sp);
		}

	}

	private class StartChatButton extends JButton implements ActionListener {
		private static final long serialVersionUID = 27L;
		private GUIstartpage sp;

		public StartChatButton(String name, GUIstartpage sp) {
			super(name);
			this.sp = sp;
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String title = "PrivateChat";
			String chatFriend = null;
			String s = JOptionPane.showInputDialog(null,
					"Enter the name of the private chat room : ",
					"Private Chatroom", 1);
			if (s == null) {
				return;
			} else {
				title += s;
				client.send("C:" + title);
				privteChatrooms.put(title, new GUIprivate(client, username,
						title, mbftp, sp));
			}
		}

	}

	private class ExitButton extends JButton implements ActionListener {

		/**
		 * 
		 */
		private static final long serialVersionUID = 27L;

		private ExitButton(String name) {
			super(name);
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			client.send("Q");
			System.exit(1);
		}
	}

	public void mouseClicked(MouseEvent arg0) {
	}

	public void addChatroom(String chatRoom) {
		chatrooms.put(chatRoom, new GUIchatroom(username, chatRoom, client,
				this));
	}

	public void removeChatroom(String chatRoom) {
		chatrooms.remove(chatRoom);

	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
	}

	public void mouseReleased(MouseEvent arg0) {
	}

	public void addRoom(String room) {
		roomsModel.addElement(room);
	}

	public void removeRoom(String room) {
		roomsModel.removeElement(room);
		if (chatrooms.containsKey(room)) {
			chatrooms.remove(room);
		}

	}

	public void updateAllNames(String[] names) {
		for (String n : names) {
			if (!friendsModel.contains(n) && !n.equals("Protocol::AllNames")) {
				friendsModel.addElement(n);
			}
		}

	}

	public void addName(String name) {
		friendsModel.addElement(name);
	}

	public void removeName(String name) {
		friendsModel.removeElement(name);
	}

	public void updateRoom(String[] names) {
		GUIchatroom cr = null;
		if (chatrooms.containsKey(names[1])) {
			cr = chatrooms.get(names[1]);
		}
		if (cr != null) {
			cr.reset();
			for (int i = 2; i < names.length; i++) {
				cr.addName(names[i]);
			}
		}

	}

	public void displayMessage(String room, String message) {
		if (room.contains("PrivateChat")) {
			privteChatrooms.get(room).displayMessage(message);
		} else {
			chatrooms.get(room).displayMessage(message);
		}
	}

	public void closeRoom(String room) {
		privteChatrooms.get(room).setVisible(false);
		privteChatrooms.get(room).dispose();
		privteChatrooms.remove(room);
	}

	public void removeRoomFromLists(String title) {
		if (chatrooms.containsKey(title)) {
			chatrooms.remove(title);
		} else {
			privteChatrooms.remove(title);
		}

	}

}
